/**
 * @file   UInt8.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-19
 * @brief  DON data type object: UInt8
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/DataType/UInt8.h>
#include <QtCore/QByteArray>

using namespace DON::Core::DataType;

class UInt8::Private
{
public:
    Private()
        : value(0u)
    { }

    quint8 value;
};

UInt8::UInt8()
    : AbstractDataType(),
      d(new Private())
{
}

UInt8::UInt8(const UInt8 &other)
    : AbstractDataType(),
      d(new Private())
{
    d->value = other.d->value;
}

UInt8::~UInt8()
{
    delete d;
}

UInt8 & UInt8::operator=(const UInt8 &other)
{
    d->value = other.d->value;

    return *this;
}

AbstractDataType * UInt8::clone() const
{
    return new UInt8(*this);
}

DataType UInt8::getDataType() const
{
    return DataType_UInt8;
}

quint8 UInt8::getValue() const
{
    return d->value;
}

void UInt8::setValue(const quint8 value)
{
    d->value = value;
}

bool UInt8::fromBinary(const QByteArray &data, const int startIndex, int *size)
{
    quint8 value = 0;
    bool success = false;

    if ((data.size() > 0) &&
        (startIndex >= 0) &&
        (startIndex < data.size()))
    {
        value = UInt8::fromBinary(data.at(startIndex));
        success = true;
    }

    if (success)
    {
        d->value = value;

        if (size != NULL)
        {
            *size = 1;
        }
    }

    return success;
}

QByteArray UInt8::toBinary() const
{
    return QByteArray(1, UInt8::toBinary(d->value));
}

UInt8 * UInt8::fromAbstractDataType(AbstractDataType *instance)
{
    UInt8 *outputInstance = NULL;

    if (instance != NULL)
    {
        if (instance->getDataType() == DataType_UInt8)
        {
            outputInstance = static_cast<UInt8 *>(instance);
        }
    }

    return outputInstance;
}

const UInt8 * UInt8::fromAbstractDataType(const AbstractDataType *instance)
{
    const UInt8 *outputInstance = NULL;

    if (instance != NULL)
    {
        if (instance->getDataType() == DataType_UInt8)
        {
            outputInstance = static_cast<const UInt8 *>(instance);
        }
    }

    return outputInstance;
}

quint8 UInt8::fromBinary(const char data)
{
    return static_cast<quint8>(data);
}

char UInt8::toBinary(const quint8 value)
{
    return static_cast<char>(value);
}
